package com.shj.em.biz.prod.controller;

import com.shj.em.base.exception.BizException;
import com.shj.em.base.exception.EmCodeMsg;
import com.shj.em.biz.prod.entity.OrderCnc;
import com.shj.em.biz.prod.service.OrderCncService;
import com.shj.em.biz.report.dto.EmpEffortsDTO;
import com.shj.em.biz.sys.entity.Cd;
import com.shj.em.biz.hr.entity.Emp;
import com.shj.em.biz.sys.service.CdService;
import com.shj.em.biz.hr.service.EmpService;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.shj.em.common.annotation.Log;
import com.shj.em.base.support.ResMsg;
import com.shj.em.base.vo.PageVO;

import com.shj.em.biz.prod.entity.MachineEffort;
import com.shj.em.biz.prod.service.MachineEffortService;
import lombok.extern.slf4j.Slf4j;
import com.shj.em.base.controller.BaseController;


/**
 * <p>
 * 订单工时表 前端控制器
 * </p>
 *
 * @author Shen Huang Jian
 * @since 2018-11-27
 */
@Controller
@Slf4j
@RequestMapping("/machineEffort")
public class MachineEffortController extends BaseController<MachineEffort>{

    private String prefix = "prod/machineeffort";

    @Autowired
    MachineEffortService machineeffortService;

    @Autowired
    OrderCncService ordercncService;

    @Autowired
    EmpService empService;

    @GetMapping("/gotoAddEffort/{orderId}/{cncId}")
    public String gotoAddEffort(@PathVariable("orderId") Long orderId,
                                @PathVariable("cncId") Long cncId, Model model){
        OrderCnc orderCnc = ordercncService.selectById(cncId);
        Emp emp = empService.selectById(orderCnc.getEmpId());
        if(emp != null){
            orderCnc.setEmpName(emp.getEmpName());
        }
        model.addAttribute("ordercnc", orderCnc);
        model.addAttribute("orderId", orderId);
        return prefix + "/search";
    }

    @GetMapping("/gotoAddOrViewEffort/{orderId}/{cncId}")
    public String gotoAddOrViewEffort(@PathVariable("orderId") Long orderId,
                                @PathVariable("cncId") Long cncId, Model model){
        OrderCnc orderCnc = ordercncService.selectById(cncId);
        Emp emp = empService.selectById(orderCnc.getEmpId());
        if(emp != null){
            orderCnc.setEmpName(emp.getEmpName());
        }
        model.addAttribute("ordercnc", orderCnc);
        model.addAttribute("orderId", orderId);

        //TODO 暂时允许修改
//        if(orderCnc.isFinishFlag()){
//            return prefix + "/view";
//        }else{
            return prefix + "/search";
//        }

    }

    @GetMapping("/list")
    @ResponseBody
    public PageVO<MachineEffort> list(@RequestParam("programId") Long programId){
        List<MachineEffort> list = machineeffortService.findMachineEffortByProgramId(programId);
        PageVO<MachineEffort> pageVO = new PageVO<>();
        pageVO.setTotal(list.size());
        pageVO.setRows(list);
        return pageVO;
    }

    @GetMapping("/gotoAdd/{orderId}/{programId}")
    public String gotoAdd(@PathVariable("orderId") Long orderId,
                   @PathVariable("programId") Long programId, Model model){
        model.addAttribute("isEdit", false);

        MachineEffort machineEffort = new MachineEffort();
        machineEffort.setOrderId(orderId);
        machineEffort.setProgramId(programId);
        machineEffort.setShiftFlag(false);
        addPageInfo(model, machineEffort);

        return prefix + "/add";
    }

    private void addPageInfo(Model model, MachineEffort machineEffort){
        model.addAttribute("machineeffort", machineEffort);
        List<Cd> workShifts = cdService.queryCdsByType(CdService.CD_TYPE_SHIFT);
        model.addAttribute("workShifts", workShifts);

        List<EmpEffortsDTO> emps = empService.findAllProdGroupOneEmps();
        model.addAttribute("emps", emps);

        if(emps == null || emps.isEmpty()){
            throw new BizException(EmCodeMsg.EMP_NOT_FOUND);
        }

        model.addAttribute("deptId", emps.get(0).getDeptId());
    }

    @GetMapping("/gotoEdit/{id}")
    String gotoEdit(@PathVariable("id") Long id, Model model){
        MachineEffort machineeffort = machineeffortService.selectById(id);
        addPageInfo(model, machineeffort);
        model.addAttribute("isEdit", true);
        return prefix + "/add";
    }


	@Log("创建订单工时表")
	@ResponseBody
	@PostMapping("/add")
	public ResMsg add(MachineEffort machineeffort){
        machineeffortService.insert(machineeffort);
        return ResMsg.ok();
	}

    @Log("更新订单工时表")
	@ResponseBody
	@PostMapping("/edit")
	public ResMsg edit(MachineEffort machineeffort){
        machineeffortService.updateById(machineeffort);
        return ResMsg.ok();
	}

	@Log("删除订单工时表")
	@PostMapping( "/remove")
	@ResponseBody
	public ResMsg remove(@RequestParam("id") Long id){
        try{
            machineeffortService.deleteById(id);
            return ResMsg.ok();
        }catch(Exception e){
            return ResMsg.error();
        }
	}

	@Log("批量删除订单工时表")
	@PostMapping( "/batchRemove")
	@ResponseBody
	public ResMsg batchRemove(@RequestParam("ids[]") Long[] ids){
        machineeffortService.deleteBatchIds(Arrays.asList(ids));
		return ResMsg.ok();
	}

}

